<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>LiquidFun Programmer&#39;s Guide: Loose Ends</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">LiquidFun Programmer&#39;s Guide
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('md__chapter12__loose__ends.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Loose Ends </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a href="#ud">User Data</a><br/>
 <a href="#id">Implicit Destruction</a><br/>
 <a href="#pcs">Pixels and Coordinate Systems</a><br/>
</p>
<p><a class="anchor" id="ud"></a> </p>
<h2>User Data</h2>
<p>The b2Fixture, b2Body, and b2Joint classes allow you to attach user data as a void pointer. This is handy when you are examining LiquidFun data structures and you want to determine how they relate to the objects in your game engine.</p>
<p>For example, it is typical to attach an actor pointer to the rigid body on that actor. This sets up a circular reference. If you have the actor, you can get the body. If you have the body, you can get the actor.</p>
<p>&#160;&#160;&#160;<code>GameActor* actor = GameCreateActor();</code><br/>
 &#160;&#160;&#160;<code>b2BodyDef bodyDef;</code><br/>
 &#160;&#160;&#160;<code>bodyDef.userData = actor;</code><br/>
 &#160;&#160;&#160;<code>actor-&gt;body = box2Dworld-&gt;CreateBody(&amp;bodyDef);</code><br/>
</p>
<p>Here are some examples of cases where you would need the user data:</p>
<ul>
<li>Applying damage to an actor using a collision result.</li>
<li>Playing a scripted event if the player is inside an axis-aligned box.</li>
<li>Accessing a game structure when LiquidFun notifies you that a joint is going to be destroyed.</li>
</ul>
<p>Keep in mind that user data is optional and you can put anything in it. However, you should be consistent. For example, if you want to store an actor pointer on one body, you should keep an actor pointer on all bodies. Don't store an actor pointer on one body, and a foo pointer on another body. Casting an actor pointer to a foo pointer may lead to a crash.</p>
<p>User data pointers are NULL by default.</p>
<p>For fixtures you might consider defining a user data structure that lets you store game specific information, such as material type, effects hooks, sound hooks, etc.</p>
<p>&#160;&#160;&#160;<code>struct FixtureUserData</code><br/>
 &#160;&#160;&#160;<code>{</code><br/>
 &#160;&#160;&#160;&#160;&#160;&#160;<code>int materialIndex;</code><br/>
 &#160;&#160;&#160;&#160;&#160;&#160;<code>. . .</code><br/>
 &#160;&#160;&#160;<code>};</code><br/>
 &#160;&#160;&#160;<code>FixtureUserData myData = new FixtureUserData;</code><br/>
 &#160;&#160;&#160;<code>myData-&gt;materialIndex = 2;</code><br/>
 &#160;&#160;&#160;<code>b2FixtureDef fixtureDef;</code><br/>
 &#160;&#160;&#160;<code>fixtureDef.shape = </code><br/>
 &#160;&#160;&#160;<code>fixtureDef.userData = myData;</code><br/>
 &#160;&#160;&#160;<code>b2Fixture* fixture = body-&gt;CreateFixture(&amp;fixtureDef);</code><br/>
 &#160;&#160;&#160;<code>. . .</code><br/>
 &#160;&#160;&#160;<code>delete fixture-&gt;GetUserData();</code><br/>
 &#160;&#160;&#160;<code>fixture-&gt;SetUserData(NULL);</code><br/>
 &#160;&#160;&#160;<code>body-&gt;DestroyFixture(fixture);</code><br/>
</p>
<p><a class="anchor" id="id"></a> </p>
<h2>Implicit Destruction</h2>
<p>LiquidFun doesn't use reference counting. So if you destroy a body it is really gone. Accessing a pointer to a destroyed body has undefined behavior. In other words, your program will likely crash and burn. To help fix these problems, the debug build memory manager fills destroyed entities with FDFDFDFD. This can help find problems more easily in some cases.</p>
<p>If you destroy a LiquidFun entity, it is up to you to make sure you remove all references to the destroyed object. This is easy if you only have a single reference to the entity. If you have multiple references, you might consider implementing a handle class to wrap the raw pointer.</p>
<p>Often when using LiquidFun you will create and destroy many bodies, shapes, and joints. Managing these entities is somewhat automated by LiquidFun. If you destroy a body then all associated shapes and joints are automatically destroyed. This is called implicit destruction.</p>
<p>When you destroy a body, all its attached shapes, joints, and contacts are destroyed. This is called implicit destruction. Any body connected to one of those joints and/or contacts is woken. This process is usually convenient. However, you must be aware of one crucial issue:</p>
<p>Caution</p>
<p>When a body is destroyed, all fixtures and joints attached to the body are automatically destroyed. You must nullify any pointers you have to those shapes and joints. Otherwise, your program will die horribly if you try to access or destroy those shapes or joints later.</p>
<p>To help you nullify your joint pointers, LiquidFun provides a listener class named b2DestructionListener that you can implement and provide to your world object. Then the world object will notify you when a joint is going to be implicitly destroyed</p>
<p>Note that there no notification when a joint or fixture is explicitly destroyed. In this case ownership is clear and you can perform the necessary cleanup on the spot. If you like, you can call your own implementation of b2DestructionListener to keep cleanup code centralized.</p>
<p>Implicit destruction is a great convenience in many cases. It can also make your program fall apart. You may store pointers to shapes and joints somewhere in your code. These pointers become orphaned when an associated body is destroyed. The situation becomes worse when you consider that joints are often created by a part of the code unrelated to management of the associated body. For example, the testbed creates a b2MouseJoint for interactive manipulation of bodies on the screen.</p>
<p>LiquidFun provides a callback mechanism to inform your application when implicit destruction occurs. This gives your application a chance to nullify the orphaned pointers. This callback mechanism is described later in this manual.</p>
<p>You can implement a b2DestructionListener that allows b2World to inform you when a shape or joint is implicitly destroyed because an associated body was destroyed. This will help prevent your code from accessing orphaned pointers.</p>
<p>&#160;&#160;&#160;<code>class MyDestructionListener : public b2DestructionListener</code><br/>
 &#160;&#160;&#160;<code>{</code><br/>
 &#160;&#160;&#160;&#160;&#160;&#160;<code>void SayGoodbye(b2Joint* joint)</code><br/>
 &#160;&#160;&#160;&#160;&#160;&#160;<code>{</code><br/>
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>// remove all references to joint.</code><br/>
 &#160;&#160;&#160;&#160;&#160;&#160;<code>}</code><br/>
 &#160;&#160;&#160;<code>};</code><br/>
</p>
<p>You can then register an instance of your destruction listener with your world object. You should do this during world initialization.</p>
<p>&#160;&#160;&#160;<code>myWorld-&gt;SetListener(myDestructionListener);</code></p>
<p><a class="anchor" id="pcs"></a> </p>
<h2>Pixels and Coordinate Systems</h2>
<p>Recall that LiquidFun uses MKS (meters, kilograms, and seconds) units and radians for angles. You may have trouble working with meters because your game is expressed in terms of pixels. To deal with this in the testbed I have the whole <em>game</em> work in meters and just use an OpenGL viewport transformation to scale the world into screen space.</p>
<p>&#160;&#160;&#160;<code>float lowerX = -25.0f, upperX = 25.0f, lowerY = -5.0f, upperY = 25.0f;</code><br/>
 &#160;&#160;&#160;<code>gluOrtho2D(lowerX, upperX, lowerY, upperY);</code><br/>
</p>
<p>If your game must work in pixel units then you should convert your length units from pixels to meters when passing values from LiquidFun. Likewise you should convert the values received from LiquidFun from meters to pixels. This will improve the stability of the physics simulation.</p>
<p>You have to come up with a reasonable conversion factor. I suggest making this choice based on the size of your characters. Suppose you have determined to use 50 pixels per meter (because your character is 75 pixels tall). Then you can convert from pixels to meters using these formulas:</p>
<p>&#160;&#160;&#160;<code>xMeters = 0.02f * xPixels;</code><br/>
 &#160;&#160;&#160;<code>yMeters = 0.02f * yPixels;</code><br/>
</p>
<p>In reverse:</p>
<p>&#160;&#160;&#160;<code>xPixels = 50.0f * xMeters;</code><br/>
 &#160;&#160;&#160;<code>yPixels = 50.0f * yMeters;</code><br/>
</p>
<p>You should consider using MKS units in your game code and just convert to pixels when you render. This will simplify your game logic and reduce the chance for errors since the rendering conversion can be isolated to a small amount of code.</p>
<p>If you use a conversion factor, you should try tweaking it globally to make sure nothing breaks. You can also try adjusting it to improve stability.</p>
<p><em>This content is licensed under <a href="http://creativecommons.org/licenses/by/4.0/legalcode">Creative Commons Attribution 4.0</a>. For details and restrictions, please see the <a href="md__content_license.html">Content License</a>.</em> </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-46159502-1', 'auto');
ga('create', 'UA-49880327-7', 'auto', {'name': 'liquidFunTracker'});
ga('send', 'pageview');
ga('liquidFunTracker.send', 'pageview');
</script>
</body>
</html>
